import 'aplayer/dist/APlayer.min.css'
import APlayer from 'aplayer'
const state = {
  useName: 'sam',
  homeaudio: '',
  audioplayer: '',
  time: '',
  audioState: '',
  num: 0,
  audiostate: false,
  cur: '',
  select: ''
}
let time
const mutations = {
  homeaudio (state, audio) {
    // clearInterval(time)
    if (state.homeaudio === audio) {
      // if (state.audioplayer.audio.paused) {
      //   zhuan()
      // } else {
      //   clearInterval(time)
      // }
      return false
    } else {
      state.homeaudio = audio
      if (state.audioplayer === '') {
        initaplayer()
      } else {
        state.cur = 0
        state.audioplayer.audio.src = state.homeaudio
        state.audioplayer.list.remove(0)
        state.audioplayer.list.add({
          name: 'name',
          artist: 'artist',
          url: state.homeaudio,
          cover: 'cover.jpg'
        })
      }
    }
    state.num = 0
    // if (state.cur !== '' && document.querySelector('.logo')) {
    //   zhuan()
    // } else {
    //   clearInterval(time)
    // }
  },
  setselect (state, id) {
    state.select = id
  }
}
const actions = {
  cleartime (vux, audio) {
    // alert(123321)
    console.log('我是执行清楚定时器')
    clearInterval(time)
  }
}
const getters = {
  vuexCurrentTime: state => { // 通过方法访问
    return state.cur
  }
}
let initaplayer = () => {
  state.audioplayer = new APlayer({
    container: document.getElementById('aplayer'),
    lrcType: 1,
    loop: 'none',
    preload: 'none',
    audio: [{
      name: 'name',
      artist: 'artist',
      url: state.homeaudio,
      cover: 'demo.jpg'
    }]
  })
  state.audioplayer.on('timeupdate', function () {
    state.cur = state.audioplayer.audio.currentTime
    // if (state.audioplayer.audio.currentTime === state.audioplayer.audio.duration) {
    //   // state.audioplayer.destroy()
    // }
  })
  state.audioplayer.on('play', function () {
    zhuan()
  })
  state.audioplayer.on('pause', function () {
    clearInterval(time)
  })
}
export function zhuan () {
  if (document.querySelector('.logo')) {
    time = setInterval(() => {
      state.num++
      document.querySelector('.logo').style.transform = `rotateZ(${state.num}deg)`
    }, 100)
  } else {
    clearInterval(time)
  }
}
// 不要忘记把state, mutations等暴露出去。
export default {
  state,
  mutations,
  actions,
  getters
}
